<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="主播标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入主播标题"
          clearable
        />
      </el-form-item>
      <el-form-item label="主播名称" prop="advMembersName">
        <el-input
          v-model="queryParams.advMembersName"
          placeholder="请输入主播名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="直播开始时间" prop="begintime">
        <el-date-picker
          v-model="queryParams.begintime"
          type="date"
          placeholder="请选择直播开始时间"
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item label="直播开始时间" prop="endtime">
        <el-date-picker
          v-model="queryParams.endtime"
          type="date"
          placeholder="请选择直播结束时间"
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item label="主播状态" prop="isAudit">
        <el-select v-model="queryParams.isAudit" placeholder="会员状态" clearable>
          <el-option 
              v-for="dict in dict.type.adv_income_is_audit"
              :label="dict.label" 
              :value="dict.value"
              :key="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="主播状态" prop="broadstatus">
        <el-select v-model="queryParams.broadstatus" placeholder="会员状态" clearable>
          <el-option 
              v-for="dict in dict.type.adv_broadcast_broadcast_status"
              :label="dict.label" 
              :value="dict.value"
              :key="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete2"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >按场次排序</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="直播标题" align="center" prop="title" />
      <el-table-column label="直播内容" align="center" prop="content" show-overflow-tooltip/>
      <el-table-column label="主播人" prop="advMembersId" >
        <template slot-scope="scope">
          {{ getMembersNickName(scope.row.advMembersId) }}
        </template>
      </el-table-column>
      <el-table-column label="直播链接" align="center" prop="broadUrl" />
      <el-table-column label="直播人数" align="center" prop="persons" />
      <!-- <el-table-column label="直播场数" align="center" prop="roomCount" /> -->
      <el-table-column label="开始时间" align="center" prop="begintime" />
      <el-table-column label="直播状态" align="center" prop="broadstatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.adv_broadcast_broadcast_status" :value="scope.row.broadstatus"/>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endtime" />
      <el-table-column label="审核状态" align="center" prop="isAudit" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.adv_income_is_audit" :value="scope.row.isAudit"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-s-home"
            v-hasPermi="['system:role:edit']"
          >推荐首页</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="handleSolid(scope.row, 1)"
            v-hasPermi="['system:role:remove']"
            v-if="scope.row.isAudit == '0'"
          >审核通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleSolid(scope.row, 2)"
            v-hasPermi="['system:role:remove']"
            v-if="scope.row.isAudit == '0'"
          >审核拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    
     
    <pagination
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"
  />

    <!-- 添加或修改用户和角色关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="直播标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入主播标题" clearable />
        </el-form-item>
        <el-form-item label="封面图片" prop="image">
          <image-upload v-model="form.image" limit="1"/>
        </el-form-item>
        <el-form-item label="直播内容" prop="content">
          <el-input type="textarea" v-model="form.content" placeholder="请输入主播内容" clearable />
        </el-form-item>
        <el-form-item label="主播人" prop="advMembersId" >
          <el-select v-model="form.advMembersId" placeholder="主播人"  style="width: 380px">
              <el-option
                  v-for="item in broadList"
                  :key="item.advMembersId"
                  :label="item.memberName"
                  :value="item.advMembersId">
              </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="主播链接" prop="broadUrl">
          <el-input v-model="form.broadUrl" placeholder="请输入主播链接" clearable />
        </el-form-item> -->
        <!-- <el-form-item label="直播人数" prop="persons">
          <el-input v-model="form.persons" placeholder="请输入直播人数" clearable />
        </el-form-item> -->
        <el-form-item label="直播状态" prop="broadstatus" v-if="this.title !='添加直播'">
          <el-select v-model="form.broadstatus" placeholder="请选择直播状态" style="width: 380px">
            <el-option 
              v-for="dict in dict.type.adv_broadcast_broadcast_status"
              :key="dict.value"
              :label="dict.label" 
              :value="dict.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="停播原因" prop="content" v-if="form.broadstatus == 3"> 
          <el-input type="textarea" v-model="form.reason" placeholder="请输入停播原因" clearable />
        </el-form-item>
        <el-form-item label="开始时间" prop="begintime">
          <el-date-picker
            v-model="form.begintime"
            type="datetime"
            placeholder="请选择直播开始时间"
            style="width: 380px"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endtime">
          <el-date-picker
            v-model="form.endtime"
            type="datetime"
            placeholder="请选择直播结束时间"
            style="width: 380px"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBroadcast, getBroadcast, addBroadcast, updateBroadcast, delBroadcast } from "@/api/server/broadcast.js"
import { listMembers, addMembers, updateMembers, delMembers, listAnchor } from '@/api/server/members.js'
export default {
  dicts: ['adv_broadcast_broadcast_status', 'adv_income_is_audit'],
  data() {
    return {
      broadList: {},
      membersList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickname: "",
        username: "",
        parentUsername: "",
        status: "",
        isAnchor: ""
      },
      showSearch: true,
      single: false,
      multiple: false,
      loading: false,
      currentPage4: 1,
      roleList: [],
      total: 0,
      title: "",
      open: false,
      form: {},
      rules: {
        title: [
          { required: true, message: "请输入主播标题", trigger: "blur" }
        ],
        content: [
          { required: true, message: "请输入主播内容", trigger: "blur" }
        ],
        broadUrl: [
          { required: true, message: "请输入主播链接", trigger: "blur" }
        ],
        broadstatus: [
          { required: true, message: "请选择直播状态", trigger: "change" }
        ],
        advMembersId: [
          { required: true, message: "选择直播人", trigger: "change" }
        ],
        persons: [
          { required: true, message: "请输入直播人数", trigger: "change" }
        ],
        begintime: [
          { required: true, message: "请选择开始时间", trigger: "change" }
        ],
        endtime: [
          { required: true, message: "请选择结束时间", trigger: "change" }
        ],
        image: [
          { required: true, message: "请选择封面图片", trigger: "blur" }
        ]
      },
    }
  },
  created() {
    this.getList();
    this.getMembers();
    this.listAnchor();
  },
  methods: {
    handleDelete2(){
      this.$confirm('此操作将永久删除该直播, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBroadcast(this.ids.join(',')).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        }).catch((error) => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    listAnchor(){
      listAnchor().then(result => {
        this.broadList = result.data;
        console.log("主播信息",result);
      })
    },
     // 获取所有会员
     getMembers() {
      listMembers(this.queryParams).then(result => {
        this.membersList = result.rows
      })
    },
    // 根据id获得会员名称
    getMembersNickName(id) {
      for (var i = 0; i < this.broadList.length; i++) {
        if(this.broadList[i].advMembersId == id) {
          return this.broadList[i].memberName
        }
      }
    },
    handleQuery() {
      if(this.queryParams.advMembersName!=null){
        for (var i = 0; i < this.broadList.length; i++) {
          if(this.broadList[i].memberName == this.queryParams.advMembersName) {
             this.queryParams.advMembersId = this.broadList[i].advMembersId
          }
        }
      }
      this.getList();
    },
    resetQuery() {},
    handleExport() {
      this.download('/system/broadcast/export', {
        ...this.queryParams
      }, `直播列表_${new Date().getTime()}.xlsx`)
    },
    getList() {
      listBroadcast(this.queryParams).then((response) => {
        this.roleList = response.rows;  
        this.total = response.total;
      })
    },
    handleSelectionChange() {},
    cancel() {
      this.getList();
      this.open = false;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.advBroadcastId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加直播";
    },
    handleUpdate(row) {
      this.reset();
      row.broadstatus = row.broadstatus + ''
      this.form = row;
      console.log(row)
      this.open = true;
      this.title = "修改直播";
    },
    reset() {
      this.form = {
        nickname: "",
        username: "",
        parentUsername: "",
        status: "",
        isAnchor: ""
      }
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该直播, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBroadcast(row.advBroadcastId).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.title === '添加直播'){
            this.form.broadstatus = '2';
            addBroadcast(this.form).then((response) => {
              this.$message({
                type: 'success',
                message: '提交成功!'
              });
              this.getList();
              this.open = false;
            })
          }
          if(this.title === '修改直播'){
            updateBroadcast(this.form).then((response) => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.getList();
              this.open = false;
            })
          }
        } else {
          this.$message({
            type: 'error',
            message: '表单验证失败!'
          });
        }
      })
    },
    handleSolid(row, status) {
      this.$confirm('审核', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.isAudit = status;
        updateBroadcast(row).then((response) => {
          this.$message({
            type: 'success',
            message: '审核成功!'
          });
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    handleStop(row) {
      this.$confirm('此操作将永久禁用该主播, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '禁用成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消禁用'
        });
      });
    },
    
  }
}
</script>

<style>

</style>

